package com.hundsun.yr.universal.p2p.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.data.HSSharedPreferences;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils.NetType;
import com.hundsun.yr.universal.library.widget.SwitchButton;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Config;
import com.hundsun.yr.universal.p2p.config.Config.SPPath;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.widget.ItemView;

/**
 * 设置-设置手势密码
 * 
 * @author tonglj
 * 
 */
public class SetGesturePassActivity extends BaseActivity implements OnClickListener {

	private SwitchButton mSwitchBtn;
	private ItemView mItemView;
	/**
	 * 存储手势密码结果
	 */
	private HSSharedPreferences mConfigSP;
	/**
	 * 手势密码结果
	 */
	private String gesturelockResult;

	/**
	 * 是否开启手势密码
	 */
	private boolean ifGesture;
	

	private int gestureOpen = 1;
	private int gestureClose = 2;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_update_updategesturelock:
			// Bundle bundle = new Bundle();
			// bundle.putString("destination", "GestureLockUpdateActivity");
			// readyGo(GestureLockLoginActivity.class, bundle);
			readyGoForResult(GestureLockVerifyActivity.class,1);
			break;

		default:
			break;
		}
	}

	@Override
	protected boolean isApplyKitKatTranslucency() {
		return true;
	}

	@Override
	protected int getContentViewLayoutID() {
		return R.layout.act_set_gesturepass;
	}

	@Override
	protected void initViewsAndEvents() {
		findView();
		mConfigSP = new HSSharedPreferences(mContext, SPPath.file_configInfo);
		gesturelockResult = mConfigSP.getShareProf(sharedPreferences.getShareProf(UserInfo.USER_NO) + Config.GESTURE);
		ifGesture = mConfigSP.getBoolean(sharedPreferences.getShareProf(UserInfo.USER_NO) + Config.IF_GESTURE);
		if (ifGesture) {
			mSwitchBtn.setChecked(true);
			mItemView.setVisibility(View.VISIBLE);
		} else {
			mSwitchBtn.setChecked(false);
			mItemView.setVisibility(View.GONE);
		}
		setToolbarTitle(R.string.gesturelock);
		mSwitchBtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					if (TextUtils.isEmpty(gesturelockResult)) {//未设置过手势密码，跳转到手势密码界面
						SetGesturePassActivity.this.readyGoForResult(GestureLockActivity.class,1);
					} else {
						Bundle data = new Bundle();
						data.putInt("openflag", gestureOpen);
						readyGoForResult(GestureLockVerify2Activity.class,1,data);
						mItemView.setVisibility(View.VISIBLE);
					}
				} else {
					Bundle data = new Bundle();
					data.putInt("openflag", gestureClose);
					readyGoForResult(GestureLockVerify2Activity.class,1,data);
					mItemView.setVisibility(View.GONE);
				}
			}
		});
		mItemView.setOnClickListener(this);
	}

	private void findView() {
		mSwitchBtn = findView(R.id.switchb_setgesturepass);
		mItemView = (ItemView) findView(R.id.iv_update_updategesturelock);
	}

//	@Override
//	protected void onResume() {
//		gesturelockResult = mConfigSP.getShareProf(sharedPreferences.getShareProf(UserInfo.USER_NO) + Config.GESTURE);
//		ifGesture = mConfigSP.getBoolean(sharedPreferences.getShareProf(UserInfo.USER_NO) + Config.IF_GESTURE);
//		if (TextUtils.isEmpty(gesturelockResult)) {
//			mSwitchBtn.setChecked(false);
//		} else if (ifGesture) {
//			mSwitchBtn.setChecked(true);
//			mItemView.setVisibility(View.VISIBLE);
//		} else {
//			mSwitchBtn.setChecked(false);
//		}
//		super.onResume();
//	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		Intent intent = new Intent(SetGesturePassActivity.this,SetGesturePassActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
	}
	
	@Override
	protected void getBundleExtras(Bundle extras) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onEveComming(EventCenter eventCenter) {
		// TODO Auto-generated method stub

	}

	@Override
	protected boolean isBindEventBusHere() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected View getLoadingTargetView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void onNetworkConnected(NetType type) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onNetworkDisConnected() {
		// TODO Auto-generated method stub

	}

	@Override
	protected boolean isApplyStatusBarTranslucency() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected boolean toggleOverridePendingTransition() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected TransitionMode getOverridePendingTransitionMode() {
		// TODO Auto-generated method stub
		return null;
	}

}
